package com.example.personalmemo.slice;

import com.example.personalmemo.ResourceTable;
import com.example.personalmemo.Util.ResultUtil;
import com.example.personalmemo.dataName.DataAbilityName;
import com.example.personalmemo.recordInfo.MyItem;
import com.example.personalmemo.recordInfo.RecordInfo;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TextField;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "Demo");
    private ListContainer listContainer;

    DataAbilityPredicates predicates;
    private DataAbilityHelper dataAbilityHelper;
    List<RecordInfo> list;
    MyItem provider;


    //    private Component mArBtn;
    private TextField mQueTextfeild;
    private Button mQueBtn;

    @Override
    public void onStart(Intent intent) {
        HiLog.info(LABEL_LOG, "MainAbilitySlice onStart");

        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        dataAbilityHelper = DataAbilityHelper.creator(this);

        initCompent();

        list = new ArrayList<>();

        provider = new MyItem(list, this);
        listContainer.setItemProvider(provider);
    }

    private void queryDatas() {

        predicates = new DataAbilityPredicates();
        // 获取文本值
        String numberStr = mQueTextfeild.getText();
        if (numberStr.length() != 0 && numberStr != null && !"".equals(numberStr)) {
            predicates.equalTo("title", numberStr);
        }

        try {
            ResultSet resultSet = dataAbilityHelper.query(Uri.parse(DataAbilityName.DATA_URI + "/infodata/queryList"), null, predicates);

            list = ResultUtil.getRecordList(resultSet);
            provider.setList(list);
            provider.notifyDataChanged();

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }


    }

    private void initCompent() {
        HiLog.info(LABEL_LOG, "initCompent");

        listContainer = findComponentById(ResourceTable.Id_que_container);
        // item点击
        listContainer.setItemClickedListener(itemClickedListener);

        findComponentById(ResourceTable.Id_img_add).setClickedListener(component ->
                present(new RecordPageSlice(), new Intent()));

        // 搜索文本
        mQueTextfeild = findComponentById(ResourceTable.Id_query_textfeild);
        // 搜索
        mQueBtn = findComponentById(ResourceTable.Id_query_btn);
        mQueBtn.setClickedListener(component -> queryDatas());


//        mArBtn = findComponentById(ResourceTable.Id_ar_btn);
//        mArBtn.setClickedListener(component -> jump());

    }

    // item 点击回调
    ListContainer.ItemClickedListener itemClickedListener = new ListContainer.ItemClickedListener() {
        @Override
        public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
            // 获取当前点击的item的 标题 和 内容
            RecordInfo recordInfo = list.get(i);
            String title = recordInfo.getTitle();
            String textinfo = recordInfo.getTextinfo();
            HiLog.info(LABEL_LOG,"title:"+title+" textinfo:"+textinfo);
//  点击跳转 到编写页面
            jump(title, textinfo,recordInfo.getId());

        }
    };

    private void jump(String title, String text,int index) {

        HiLog.info(LABEL_LOG, "jump");
        Intent intent = new Intent();
        Operation build = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName("com.example.personalmemo.Rewrites")
                .build();

        HiLog.info(LABEL_LOG,"jump: title:"+title+" textinfo:"+text+" index:"+index);

        intent.setOperation(build);
        intent.setParam("titlekey", title);
        intent.setParam("textkey", text);
        intent.setParam("indexkey", index);
        startAbility(intent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
